package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//TODO 需要完成 套餐的接口 根据接口文档完成 5.8号早上完成
@RestController
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐相关接口")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @PostMapping
    @ApiOperation(value = "新增套餐接口")
    @CachePut(cacheNames = "setmealcache",key = "#setmealDTO.categoryId")
    public Result insertSetmeal(@RequestBody SetmealDTO setmealDTO){
        log.info("新增的套餐为{}",setmealDTO);
        setmealService.insertSetmeal(setmealDTO);
        return Result.success();
    }
    @ApiOperation(value = "分页查询套餐")
    @GetMapping("/page")
    public Result<PageResult> pageQuery(SetmealPageQueryDTO setmealPageQueryDTO){
        log.info("查询的参数{}",setmealPageQueryDTO);
         PageResult pageResult = setmealService.pageQuery(setmealPageQueryDTO);
        return Result.success(pageResult);
    }
    @ApiOperation(value = "根据套餐id查询回显套餐")
    @GetMapping("/{id}")
    public Result<SetmealVO> getByid(@PathVariable Long id){
        SetmealVO setmealVO =setmealService.getByid(id);
        return Result.success(setmealVO);
    }

    /**
     * 修改套餐
     * @param setmealDTO
     * @return
     */
    @ApiOperation(value = "修改套餐")
    @PutMapping
    @CacheEvict(cacheNames = "setmealcache" ,key = "#setmealDTO.categoryId")
    public Result updateSetmeal(@RequestBody SetmealDTO setmealDTO){
        setmealService.updateSetmeal(setmealDTO);
        return Result.success();

    }
    @ApiOperation(value = "批量删除套餐")
    @DeleteMapping
    @CacheEvict(cacheNames = "setmealcache" ,allEntries = true)//所有都删除
    public Result deleteBarch(@RequestParam("ids") List<Long> ids){
        log.info("批量删除的id为{}",ids);
        setmealService.deleteBarch(ids);
        return Result.success();
    }
    @CacheEvict(cacheNames = "setmealcache" ,allEntries = true)//所有的缓存删除
    @ApiOperation(value = "起售，停售套餐")
    @PostMapping("/status/{status}")
    public Result updateStatus(@PathVariable Integer status, Long id){
        log.info("起售停售的id为{}",id);
        setmealService.updateStatus(status,id);
        return Result.success();
    }
}
